
/** 
 * https://leetcode.cn/problems/minimum-size-subarray-sum/description/
 * 209.长度最小的子数组 
 * medium, 王顶成 2024.9.1
 * c++  双指针
 */
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        //求解子数组
        int n =nums.size();
        if(n==0){
            return 0;
        }
            
        int ans=INT_MAX;
        int l=0,r=0;//定义左右指针
        int sum=0;
        while(r<n){
             sum+=nums[r];
             while(sum>=target){
                ans=min(ans,r-l+1);
                sum-=nums[l];
                l++;
             }
             r++;
        }
        if(ans==INT_MAX)
            return 0;
        else
            return ans;

    }
};